﻿Imports System.Configuration
Namespace Utilities
    ''' <summary>
    ''' Read and write application configuration file.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ConfigReader
        ''' <summary>
        ''' Read string value from config file
        ''' </summary>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetString(ByVal key As String) As String
            Return GetConfigValue(key)
        End Function
        ''' <summary>
        ''' Read integer value from config file
        ''' </summary>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetInteger(ByVal key As String) As Integer
            Dim ret As Integer = 0
            Dim s As String = GetConfigValue(key)
            Try
                ret = Integer.Parse(s)
            Catch ex As Exception
                ret = 0
            End Try
            Return ret
        End Function
     
        Public Shared Function SaveString(ByVal key As String, ByVal value As String) As Boolean
            Return SaveConfigValue(key, value)
        End Function
        Public Shared Function SaveString(ByVal key As String, ByVal value As String, ByVal configFIle As String) As Boolean
            Return SaveConfigValue(key, value, configFIle)
        End Function
        ''' <summary>
        ''' Read config value from lcsvista.config
        ''' </summary>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function GetConfigValue(ByVal key As String) As String
            Dim fileMap As New System.Configuration.ExeConfigurationFileMap
            Dim config As System.Configuration.Configuration
            Dim appSettings As System.Configuration.AppSettingsSection
            Dim returnValue As String = String.Empty
            Dim filePass As String = ""
            Dim sectionName As String = ""
            Try
                filePass = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) & filePass
                fileMap.ExeConfigFilename = filePass
                config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
                'appSettings
                appSettings = DirectCast(config.Sections.Item(sectionName), System.Configuration.AppSettingsSection)
                returnValue = appSettings.Settings.Item(key).Value

            Catch ex As Exception
                returnValue = String.Empty
            End Try
            Return returnValue
        End Function
        Private Shared Function SaveConfigValue(ByVal key As String, ByVal value As String) As Boolean
            Dim fileMap As New System.Configuration.ExeConfigurationFileMap
            Dim config As System.Configuration.Configuration
            Dim returnValue As String = String.Empty
            Dim filePass As String = "~/ResortManagement/resort.config" '= LcsConstants.CONFIG_FILE
            Dim sectionName As String = "appSettings" ' = LcsConstants.APPSETTING_SECTION
            Try
                filePass = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) & filePass
                fileMap.ExeConfigFilename = filePass
                config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
                'appSettings
                config.AppSettings.Settings(key).Value = value
                config.Save(ConfigurationSaveMode.Modified)
                ConfigurationManager.RefreshSection(sectionName)
            Catch ex As Exception
                returnValue = False
            End Try
            Return True
        End Function

        Private Shared Function SaveConfigValue(ByVal key As String, ByVal value As String, ByVal configFile As String) As Boolean
            Dim fileMap As New System.Configuration.ExeConfigurationFileMap
            Dim config As System.Configuration.Configuration
            Dim returnValue As String = String.Empty
            Dim filePass As String = configFile
            Dim sectionName As String = ""
            Try
                filePass = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) & filePass
                fileMap.ExeConfigFilename = filePass
                config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None)
                'appSettings
                config.AppSettings.Settings(key).Value = value
                config.Save(ConfigurationSaveMode.Modified)
                ConfigurationManager.RefreshSection(sectionName)
            Catch ex As Exception
                returnValue = False
            End Try
            Return True
        End Function
    End Class
End Namespace